/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import business.Quote;
import connection.YahooQuotesConnector;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.StringItem;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.PersistableManager;

/**
 *
 * @author André Pinto de Souza
 */
public class StockRefreshThread extends Thread {

    private long m_Minutes = 15;
    private final long m_MinuteInMiliseconds = 1000 * 60;
    protected StringItem m_StringItem;
    protected String[] m_Stocks;
    protected DateField m_DateField;

    public void run() {
        while (true) {
            try {
                StringBuffer v_StockValues = new StringBuffer();
                for (int c_Stock = 0; c_Stock < m_Stocks.length; c_Stock++) {
                    YahooQuotesConnector v_StockCSV = new YahooQuotesConnector(m_Stocks[c_Stock]);
                    Quote v_Quote = new Quote(v_StockCSV.getResponse());

                    try {
                        PersistableManager pm = PersistableManager.getInstance();
                        pm.save(v_Quote);
                    } catch (FloggyException e) {
                        e.printStackTrace();
                    }

                    v_StockValues.append("\n" + m_Stocks[c_Stock] + ": " + v_Quote.getValue());
                }
                m_StringItem.setText(v_StockValues.toString());
                m_DateField.setDate(new java.util.Date(System.currentTimeMillis()));
                Thread.sleep(m_Minutes * m_MinuteInMiliseconds);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
